<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>千锋健康</title>
        <meta name="description" content="千锋健康">
        <meta name="keywords" content="千锋健康">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="plugins/elementui/index.css">
        <link rel="stylesheet" href="css/login.css">
        <script>
            if(window.self != window.top){
                window.top.location = window.location;
            }
        </script>
    </head>
    <body class="hold-transition skin-purple sidebar-mini">
        <div id="app">
            <div class="login-container">
                <div class="loginBox">
                  <form method="post" class="login-form" action="/login" label-position="left">
                      <div class="title-container">
                        <div class="logoInfo clearfix">
                          <em class="logo"></em>
                        </div>
                      </div>
                      <div>
                        <span class="svg-container svg-container_login">
                          <span class="user"></span>
                        </span>
                        <input type="text" name="username" placeholder="请输入用户名" v-model="user.username"/>
                      </div>
                      <div>
                        <span class="svg-container">
                          <span class="username"></span>
                        </span>
                        <input type="password" name="password" placeholder="请输入密码" v-model="user.password"/>
                      </div>
                      <input type="button" style="width:100%;margin-bottom:30px;" @click="login()" value="登录"></input>
                </form>
                </div>
              </div>
        </div>
    </body>
    <!-- 引入组件库 -->
    <script src="js/vue.js"></script>
    <script src="plugins/elementui/index.js"></script>
    <script src="js/axios-0.18.0.js"></script>
<script>
    new Vue({
        el:"#app",
        data:{
            user:{
                username:"admin",
                password:"123"
            }
        },
        methods:{
            login(){
                //发送异步请求，访问8080服务器
                /**
                 * 使用Security的登录，内部走的是UsernamePasswordAuthenticationFilter过滤器
                 *  这个过滤器只拦截 post请求，请求路径必须是/login
                 *  同时内部获取用户名和密码的参数名为 username 和 password，并且要求是key=value&key2=value2形式
                 */
                axios({
                    url:"http://localhost:8080/login",
                    method:"post",
                    params: this.user
                }).then(resp=>{
                    let r = resp.data;
                    let code = r.code;
                    if (code == 200){
                        //登录成功（保存后端返回的token + 跳转页面）
                        localStorage.setItem("security_token",r.data);
                        location.href = "/pages/main.html"
                    }else{
                        //登录失败
                        alert(r.message)
                        location.href = "login.html"
                    }
                })
            }
        }
    })
</script>
</html>
